package co.yixiang.yshop.module.product.service.storeproductassure;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.module.product.controller.admin.storeproductassure.vo.StoreProductAssureCreateReqVO;
import co.yixiang.yshop.module.product.controller.admin.storeproductassure.vo.StoreProductAssureExportReqVO;
import co.yixiang.yshop.module.product.controller.admin.storeproductassure.vo.StoreProductAssurePageReqVO;
import co.yixiang.yshop.module.product.controller.admin.storeproductassure.vo.StoreProductAssureUpdateReqVO;
import co.yixiang.yshop.module.product.convert.storeproductassure.StoreProductAssureConvert;
import co.yixiang.yshop.module.product.dal.dataobject.storeproductassure.StoreProductAssureDO;
import co.yixiang.yshop.module.product.dal.mysql.storeproductassure.StoreProductAssureMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.module.product.enums.ErrorCodeConstants.STORE_PRODUCT_ASSURE_NOT_EXISTS;

/**
 * 商品保障服务 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class StoreProductAssureServiceImpl implements StoreProductAssureService {

    @Resource
    private StoreProductAssureMapper storeProductAssureMapper;

    @Override
    public Long createStoreProductAssure(StoreProductAssureCreateReqVO createReqVO) {
// 插入
        StoreProductAssureDO storeProductAssure = StoreProductAssureConvert.INSTANCE.convert(createReqVO);
        storeProductAssureMapper.insert(storeProductAssure);
// 返回
        return storeProductAssure.getId();
    }

    @Override
    public void updateStoreProductAssure(StoreProductAssureUpdateReqVO updateReqVO) {
// 校验存在
        validateStoreProductAssureExists(updateReqVO.getId());
// 更新
        StoreProductAssureDO updateObj = StoreProductAssureConvert.INSTANCE.convert(updateReqVO);
        storeProductAssureMapper.updateById(updateObj);
    }

    @Override
    public void deleteStoreProductAssure(Long id) {
// 校验存在
        validateStoreProductAssureExists(id);
// 删除
        storeProductAssureMapper.deleteById(id);
    }

    private void validateStoreProductAssureExists(Long id) {
        if (storeProductAssureMapper.selectById(id) == null) {
            throw exception(STORE_PRODUCT_ASSURE_NOT_EXISTS);
        }
    }

    @Override
    public StoreProductAssureDO getStoreProductAssure(Long id) {
        return storeProductAssureMapper.selectById(id);
    }

    @Override
    public List<StoreProductAssureDO> getStoreProductAssureList(Collection<Long> ids) {
        return storeProductAssureMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<StoreProductAssureDO> getStoreProductAssurePage(StoreProductAssurePageReqVO pageReqVO) {
        return storeProductAssureMapper.selectPage(pageReqVO);
    }

    @Override
    public List<StoreProductAssureDO> getStoreProductAssureList(StoreProductAssureExportReqVO
                                                                     exportReqVO) {
        return storeProductAssureMapper.selectList(exportReqVO);
    }

}
